package com.uni.web.utils;

import com.uni.common.utils.StringUtils;
import com.uni.web.model.ChatEntity;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangxn
 * @date 2025/2/19  23:31
 */
@Slf4j
public class ChatCompletionsUtils {

    private static String apiKey = "ac27de09-3cec-401a-b167-6f86faf98b60";

    private static String modelId = "ep-20250218224918-znhqh";

    private static String STATUS_WORKSPACE = "姓氏{}性别{}出生日期{}";


    /**
     * 对话
     * @return
     */
    public static String chatMessage(ChatEntity chatEntity){
        String chatMessage = "姓氏：" + chatEntity.getName();
        if (StringUtils.isNotEmpty(chatEntity.getSex())) {
            chatMessage = chatMessage + " 性别：" + chatEntity.getSex();
        }
        if (StringUtils.isNotEmpty(chatEntity.getTime())) {
            chatMessage = chatMessage + " 出生日期：" + chatEntity.getTime();
        }
        if (StringUtils.isNotEmpty(chatEntity.getSex())) {
            chatMessage = chatMessage + " 性别：" + chatEntity.getSex();
        }
        if (StringUtils.isNotEmpty(chatEntity.getSource())) {
            chatMessage = chatMessage + " 名字出处希望来源：" + chatEntity.getSource();
        }
        if (StringUtils.isNotEmpty(chatEntity.getMessage())) {
            chatMessage = chatMessage + " 期望寓意：" + chatEntity.getMessage();
        }
        chatMessage = chatMessage + "取一个合适的名字然后每个名字从出处、寓意、姓名学分析（音、形、义、意、数）、性格、健康、职业、事业运、成功运和总结分析,包括每个名字的打分 推荐5个名字,并且每个名字描述尽量详细，输出java json格式";
        ArkService service = ArkService.builder().apiKey(apiKey).build();
        final List<ChatMessage> messages = new ArrayList<>();
        final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包，是由字节跳动开发的 AI 人工智能助手").build();
        final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content(chatMessage).build();
        messages.add(systemMessage);
        messages.add(userMessage);
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(modelId)
                .messages(messages)
                .build();
        String result = service.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage().getContent().toString();
        // shutdown service
        service.shutdownExecutor();
        return result;
    }

    public static void StreamChatCompletionsExample() {
        ArkService service = ArkService.builder().apiKey(apiKey).build();
        System.out.println("\n----- streaming request -----");
        final List<ChatMessage> streamMessages = new ArrayList<>();
        final ChatMessage streamSystemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包，是由字节跳动开发的 AI 人工智能助手").build();
        final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些？").build();
        streamMessages.add(streamSystemMessage);
        streamMessages.add(streamUserMessage);

        ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder()
                .model(modelId)
                .messages(streamMessages)
                .build();

        service.streamChatCompletion(streamChatCompletionRequest)
                .blockingForEach(
                        choice -> {
                            if (choice.getChoices().size() > 0) {
                                System.out.print(choice.getChoices().get(0).getMessage().getContent());
                            }
                        }
                    );

            // shutdown service
        service.shutdownExecutor();


    }

    public static void main(String[] args) {
//        System.out.println(new Date());
//        ChatEntity chatEntity = new ChatEntity();
//        chatEntity.setSex("男");
//        chatEntity.setName("王");
//        chatEntity.setTime("2025-02-20");
//        chatEntity.setSource("诗经，楚辞");
//        String result = chatMessage(chatEntity);
//        System.out.println(result);
//        System.out.println(new Date());
        StreamChatCompletionsExample();
    }
}
